﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _82实现一个存整数的List
{
    internal class MyList
    {
        private int[] array;//容器
        public int Capacity { get { return array.Length; } }
        public int Count { get; private set; } //元素个数

        //构造函数,不给参数容量默认4
        public MyList(int capacity = 4)
        {
            array = new int[capacity];
        }
        //添加元素
        public void Add(int item)
        {
            //保证容量够用
            if (Count == Capacity)
            {
                //创建新数组
                int[] newArray = new int[array.Length * 2];
                //拷贝array中的元素到newArray中
                array.CopyTo(newArray, 0);
                array = newArray;
            }

            array[Count] = item;//把item放进数组
            Count++;
        }

        public int this[int index]
        {
            get
            {
                if (index < 0 || index > Count - 1) throw new Exception("哟呵,超出索引界限"); //抛出异常

                return array[index];
            }
            set
            {
                if (index < 0 || index > Count - 1) throw new Exception("哟呵,超出索引界限");

                array[index] = value;
            }
        }
    }
}